const jwt = require('jwt-simple');
const moment = require('moment');

const secret = '87f2666b-d3e1-4c57-9a1b-e1e3eff82d74';

// 加密
exports.getJwt = (payload) => {
    const expires = moment().add(7, 'days').valueOf();
    const token = jwt.encode(Object.assign(payload, { exp: expires}), secret);
    return token;
}

// 解密
exports.testJwt = (token) => {
    const decoded = jwt.decode(token, secret);
    return decoded;
}

exports.getId = (id1, id2, isInGroup = false) => {
    if (isInGroup) {
        return id2;
    } else {
        if (id1 > id2) {
            return id1 + '_' + id2;
        } else {
            return id2 + '_' + id1;
        }
    }
    
}